package com.zookeeper.util;

import com.string.widget.util.ValueWidget;
import com.swing.dialog.toast.ToastMessage;
import com.zookeeper.ZkConnect;

import java.util.List;

public class ZkOperateUtil {
    public static boolean deleteZkNode(String nodeKey, String rootPath, ZkConnect zooKeeper) {
        try {
            if (!rootPath.endsWith("/")) {
                rootPath = rootPath + "/";
            }
            String fullPath = rootPath + nodeKey;
            List<String> zNodes = zooKeeper.getZk().getChildren(fullPath, true);
            if (!ValueWidget.isNullOrEmpty(zNodes)) {//不是普通的节点,是目录
                ToastMessage.toast("目录不为空,不允许直接删除:" + nodeKey, 2000, java.awt.Color.RED);
                return true;
            }
            zooKeeper.deleteNode(fullPath);
            ZkConnect.clearCache(rootPath);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            ToastMessage.toast("删除失败:" + nodeKey, 2000, java.awt.Color.RED);
        }
        return false;
    }

}
